<template>
  <div class="about-master-container" :state="state">
    <base-breadcrumb :breadcrumb="!isAudit" :state="state">
      <b-steps :current="stepVal" direction="vertical" @change.self="stepChange">
        <b-step title="基本信息">
          <a-card v-if="activeKey[0]" slot="description" v-loading="loading" :bordered="false">
            <a-form-model
              ref="ruleForm"
              :model="formData"
              :rules="rules"
            >
              <a-row>
                <a-col span="8">
                  <a-form-model-item
                    label="交底单位"
                    prop="unitNameId"
                  >
                    <BaseInput
                      v-model="formData.unitName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col
                  v-show="formData.sourceId != 4"
                  span="8"
                >
                  <a-form-model-item
                    label="合同编号"
                    prop="contractCode"
                  >
                    <a-input-search
                      v-model="formData.contractCode"
                      placeholder="请选择"
                      :disabled="state === 3"
                      @search="onSearch"
                      @click="onSearch"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="合同名称"
                    prop="code"
                  >
                    <BaseInput
                      v-model="formData.contractName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col span="8">
                  <a-form-model-item label="供应商/租赁商名称">
                    <BaseInput
                      v-model="formData.supplierName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="供应商/租赁商编码">
                    <BaseInput
                      v-model="formData.supplierCode"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="交底人"
                    prop="personName"
                  >
                    <a-input-search
                      v-model="formData.personName"
                      placeholder="请选择"
                      :disabled="state === 3"
                      @search="openPerson"
                      @click="openPerson"
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col span="8">
                  <a-form-model-item
                    label="交底日期"
                    prop="confessDate"
                  >
                    <a-date-picker
                      v-model="formData.confessDate"
                      value-format="YYYY-MM-DD"
                      :disabled="state === 3"
                      type="date"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="创建人">
                    <BaseInput
                      v-model="formData.creatorName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="创建时间"
                    prop="size"
                  >
                    <BaseInput
                      v-model="formData.createTime"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-form-model>
            <div v-if="!isAudit" class="pub-next-btn">
              <a-button v-if="state !== 3" type="primary" @click="temporaryStorage()">保存草稿</a-button>
              <a-button type="primary" @click="temporaryStorage(true)">下一步</a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="交底记录">
          <a-card v-if="activeKey[1]" slot="description" :bordered="false">
            <div class="pub-next-btn">
              <record :id="formData.id" :state="state" />
              <a-button v-if="!isAudit" type="primary" @click="previous">上一步</a-button>
              <a-button v-if="!isAudit" type="primary" @click="next">下一步</a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="接底情况">
          <a-card v-if="activeKey[2]" slot="description" :bordered="false">
            <div class="pub-next-btn">
              <situation :id="formData.id" :state="state" />
              <a-button v-if="!isAudit" type="primary" @click="previous">上一步</a-button>
              <a-button v-if="!isAudit" type="primary" @click="next">下一步</a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="相关附件">
          <a-card v-if="activeKey[3]" slot="description" :bordered="false">
            <u-table
              :upload-list="addFile"
              :delete-url="fileDel"
              :is-show="state !== 3"
              :list-url="fileList"
              :business-id="formData.id"
              business-code="MM11"
            />
            <div v-if="!isAudit" class="pub-next-btn">
              <a-button type="primary" @click="previous">上一步</a-button>
            </div>
          </a-card>
        </b-step>
      </b-steps>
      <div v-if="!isAudit" class="pub-header-btn page-btn-right-top">
        <a-button v-if="state !== 3" :loading="loading" type="primary" @click="submit">提交</a-button>
        <BasePrintButton
          v-if="formData.id && state == 3"
          :before-print-fn="beforePrintFn"
          style="display: inline-block"
        />
        <a-button v-checkVisibleInDetail @click="$close">关闭</a-button>
      </div>
      <div v-else class="pub-header-btn page-btn-right-top">
        <a-button v-checkVisibleInDetail @click="$close">关闭</a-button>
      </div>
      <review ref="review" @getReview="getReview" />
      <person-list ref="personList" :other-data="{ rootOrgId: formData.unitNameId }" @getPerson="getPerson" />
    </base-breadcrumb>
  </div>
</template>
<script>
import {
  clarificaitonAddBasic,
  clarificaitonEditBasic,
  audit,
  addFile,
  fileList,
  fileDel,
  clarificaitonBasicDetail,
  print
} from '@/api/material/contract/clarificaiton'
import review from '../components/review/review'
import record from '../components/record/record'
import situation from '../components/situation/situation'

export default {
  name: 'a' + Date.now(),
  components: {
    review,
    record,
    situation
  },
  props: {
    isAudit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      beforePrintFn: async () => {
        let res = await print({ id: this.formData.id })
        // res.data = res.data.replaceAll('\n', '\\')
        return res.data.content
      },
      activeKey: [true, false, false, false, false, false, false],
      loading: false,
      stepVal: 0,
      state: Number(this.$route.query.state),
      isSubmit: false,
      levels: JSON.parse(sessionStorage.getItem('ycloud-user_info')).employee.levels,
      formData: {
        id: null,
        createTime: this.getNowTime(),
        creatorName: JSON.parse(sessionStorage.getItem('ycloud-user_info')).name,
        unitName: '',
        unitNameId: '',
        // unitName: "四川分公司",
        // unitNameId: "a41c703291e345a58951c3f0ab133775",
        contractCode: null,
        contractName: null,
        supplierName: '',
        supplierCode: '',
        personName: '',
        confessDate: null,
        contractId: ''
      },
      rules: {
        unitNameId: { required: true, message: '请选择交底单位', trigger: 'change' },
        contractCode: { required: true, message: '请选择合同编号', trigger: 'change' },
        personName: { required: true, message: '请选择交底人', trigger: 'change' },
        confessDate: { required: true, message: '请选择交底日期', trigger: 'change' }
      },
      isNext: false,
      addFile: addFile(),
      fileDel: fileDel(),
      fileList: fileList()
    }
  },
  watch: {
    isAudit: {
      immediate: true,
      handler(val) {
        if (val) {
          this.state = 3
        }
      }
    },
    stepVal(newVal, oldVal) {
      this.$set(this.activeKey, newVal, true)
    }
  },
  created() {
    if (this.$route.query.id) {
      this.formData.id = this.$route.query.id
      this.getEditInfo()
      this.isSubmit = true
    }

    if (this.state == 3) {
      this.activeKey = [true, true, true, true, false, false, false]
    }
    this.formData.unitNameId = this.$currentPost().id
    this.formData.unitName = this.$currentPost().name

  },
  methods: {
    previous: function() {
      this.isNext = true
      this.stepVal--
      if (this.stepVal < 0) {
        this.stepVal = 0
      }
    },
    openPerson: function() {
      this.$refs.personList.open()
    },
    getPerson: function(row) {
      this.formData.personName = row.name
    },
    getReview: function(selectRow) {
      this.formData.contractCode = selectRow.code
      this.formData.contractName = selectRow.name
      this.formData.supplierName = selectRow.supplier
      this.formData.supplierCode = selectRow.supplierCode
      this.formData.contractId = selectRow.id
    },
    onSearch: function(val) {
      this.$refs.review.open()
    },
    stepChange: function(val) {
      if (!this.isSubmit) {
        this.$message.warning('切换步骤前请先填写基本信息')
        this.stepVal = 0
      } else {
        if (!this.isNext) {
          this.$set(this.activeKey, val, !this.activeKey[val])
        }
      }
      this.isNext = false
    },
    // 获取编辑回显数据
    async getEditInfo() {
      this.loading = true
      const res = await clarificaitonBasicDetail({ id: this.formData.id })
      this.loading = false
      if (res.code === 200) {
        this.formData = res.data
      }
    },
    async submit() {
      if (this.isSubmit) {
        this.loading = true
        let res = await audit({ baseId: this.formData.id, auditStatus: this.formData.auditStatus })
        this.loading = false
        if (res.code === 200) {
          this.$message.success('提交成功')
          this.$close()
        }
      } else {
        this.$message.warning('提交之前请先暂存')
      }
    },
    async temporaryStorage(isNext) {
      if (this.state === 3) return this.next()
      this.$refs.ruleForm.validate(async (valid, object) => {
        if (valid) {
          this.loading = true
          let res
          if (this.formData.id) {
            res = await clarificaitonEditBasic(this.formData)
          } else {
            res = await clarificaitonAddBasic(this.formData)
          }
          this.loading = false
          if (res.code === 200) {
            this.$message.success('暂存成功')
            this.isSubmit = true
            this.formData.id = res.data.id
            this.getEditInfo()
            if (isNext) {
              this.next()
            }
          }
        } else {
          this.$message.warning("请填写完所有必填项信息")
        }
      })
    },
    next: function(val) {
      this.isNext = true
      this.stepVal++
    }
  }
}
</script>
